package com.okilo.online.app.act

import android.annotation.SuppressLint
import android.graphics.Typeface
import android.os.CountDownTimer
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.View
import com.okilo.online.app.R
import com.okilo.online.app.common.OkiloComAct
import com.okilo.online.app.databinding.OkiloActApplyWaitBinding
import com.okilo.online.app.event.OkiloEventData

/**
 * OkiloLoan
 * @Author wwdeng
 * @CreateTime: 2024/11/25 18:45
 * @Annotation：SMS 等待
 */
class OkiloApplyWaitAct: OkiloComAct() {
    private var _binding: OkiloActApplyWaitBinding? = null
    //等待时长
    private var countdown = 10
    private var countdownTimer: OkiloApplyWaitCountDownTimer? = null

    override val pageName: String = "Apply_Wait_Page"

    override fun createOkiloContentView(): View? {
        _binding = OkiloActApplyWaitBinding.inflate(layoutInflater)
        return _binding?.root
    }

    override fun initializeOkiloViews() {
        getTitleBarManager()
            .setRootBackgroundColor(getColor(R.color.okilo_color_ffeff1f3))
            .setTitleBarVisibility(View.VISIBLE)
            .setTitleVisibility(View.GONE)
            .setBackButtonVisibility(View.INVISIBLE)
            .setRightButtonIcon(R.mipmap.okilo_root_customer_icon)

        val showKey = getString(R.string.okilo_apply_wait_desc_key)
        val showText = String.format(getString(R.string.okilo_apply_wait_desc_value),showKey)
        val startIndex = showText.indexOf(showKey)
        val spannable = SpannableString(showText)
        spannable.setSpan(ForegroundColorSpan(getColor(R.color.okilo_color_ffff4d3e)), startIndex, startIndex + showKey.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        //加粗
        spannable.setSpan(StyleSpan(Typeface.BOLD), startIndex, startIndex + showKey.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        _binding?.okiloApplyWaitTipsText?.text = spannable
    }

    override fun setupOkiloEventListeners() {
        super.setupOkiloEventListeners()
    }

    override fun loadOkiloData() {
        if (countdownTimer == null){
            countdownTimer = OkiloApplyWaitCountDownTimer(this)
        }
        countdownTimer?.start()
    }

    class OkiloApplyWaitCountDownTimer(private val act: OkiloApplyWaitAct): CountDownTimer(10 * 1000, 1000) {
        override fun onTick(millisUntilFinished: Long) {
            // 这里每隔一段时间（定时器间隔）会被调用
            // 可以在这里更新UI，显示剩余时间
            act.countdown = (millisUntilFinished / 1000).toInt()
            act.sOkiloApplyWaitCountdownText()
        }
        override fun onFinish() {
            // 倒计时完成时被调用
            act.sOkiloApplyWaitCountdownFinish()
        }
    }

    /**
     * 设置文本
     */
    @SuppressLint("SetTextI18n", "DefaultLocale")
    private fun sOkiloApplyWaitCountdownText(){
        _binding?.okiloApplyWaitCountdownText?.text = String.format("%02d", countdown)
        sOkiloApplyWaitStepStatusView()
    }

    /**
     * 倒计时完成
     */
    private fun sOkiloApplyWaitCountdownFinish(){
        showOkiloLoading()
        //刷新主流程
        publishEventData(OkiloEventData(OkiloEventData.TYPE.OKILO_PROCESS_REFRESH))
    }

    /**
     * 显示状态
     */
    private fun sOkiloApplyWaitStepStatusView(){
        val binding = _binding ?: return
        // 定义步骤状态数据类
        data class StepState(val imageRes: Int, val textColor: Int)

        // 定义状态常量
        val progressState = StepState(
            R.mipmap.okilo_apply_wait_step_progress_icon,
            R.color.okilo_color_black
        )
        val finishState = StepState(
            R.mipmap.okilo_apply_wait_step_finish_icon,
            R.color.okilo_color_ffa9a9ac
        )

        // 根据countdown确定各步骤状态
        val (step1, step2, step3) = when {
            countdown > 6 -> Triple(progressState, progressState, progressState)
            countdown > 3 -> Triple(finishState, progressState, progressState)
            countdown >= 1 -> Triple(finishState, finishState, progressState)
            else -> Triple(finishState, finishState, finishState)
        }
        // 应用状态到UI
        with(binding) {
            // 步骤1
            okiloApplyWaitStepImg1.setImageResource(step1.imageRes)
            okiloApplyWaitStepText1.setTextColor(getColor(step1.textColor))

            // 步骤2
            okiloApplyWaitStepImg2.setImageResource(step2.imageRes)
            okiloApplyWaitStepText2.setTextColor(getColor(step2.textColor))

            // 步骤3
            okiloApplyWaitStepImg3.setImageResource(step3.imageRes)
            okiloApplyWaitStepText3.setTextColor(getColor(step3.textColor))
        }
    }

    override fun onOkiloPublishEvent(event: OkiloEventData) {
        super.onOkiloPublishEvent(event)
        when(event.eventType){
            OkiloEventData.TYPE.OKILO_PROCESS_REFRESHED -> {
                hideOkiloLoading()
                finish()
            }
            else -> {

            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (countdownTimer != null){
            countdownTimer?.cancel()
            countdownTimer = null
        }
    }
}